var PRODUCT_MULTI = {
	init: function() {
		this._initCheckBox();
		this._cavTotal();
		this._initEvent();
	},
	_initCheckBox: function() {
		var me = this;
		$(".myorder .mli .radio").on("click", function(e) {
			var otherMulti=$(this).parents('.multi_cart').siblings();
			if ($(this).hasClass("checked")) {
				$(this).removeClass("checked");
			} else {
				otherMulti.find('.myorder .mli .radio').removeClass('checked');
				otherMulti.find('.radio_multi').removeClass('checked');
				$(this).addClass("checked")
			}
			if ($(".myorder .mli .checked").size() == $(".myorder .mli").size()) {
				$("#checkAll").addClass("checked");
			} else {
				$("#checkAll").removeClass("checked");
			}
			var multi=$(this).parents('.multi_cart').find('.mli .radio'),
				checked_multi=$(this).parents('.multi_cart').find('.mli .radio.checked');
			if (multi.size() == checked_multi.size()) {
				$(this).parents('.multi_cart').find('.radio_multi').addClass("checked");
			} else {
				$(this).parents('.multi_cart').find('.radio_multi').removeClass("checked");
			}
			window.distributionId = $(this).data("id");
			e.stopPropagation();
			me._cavTotal();
		});
		$(".multi_cart .radio_multi").on('click',function(e){
			var multi=$(this).parents('.multi_cart').find('.mli .radio'),
				checked_multi=$(this).parents('.multi_cart').find('.mli .radio.checked'),
				otherMulti=$(this).parents('.multi_cart').siblings();
			if ($(this).hasClass("checked")) {
				$(this).removeClass("checked");
				multi.removeClass("checked");
			} else {
				otherMulti.find('.myorder .mli .radio').removeClass('checked');
				otherMulti.find('.radio_multi').removeClass('checked');
				$(this).addClass("checked");
				multi.addClass('checked');
			}
			if ($(".myorder .mli .checked").size() == $(".myorder .mli").size()) {
				$("#checkAll").addClass("checked");
			} else {
				$("#checkAll").removeClass("checked");
			}
			window.distributionId = $(this).data("id");
			e.stopPropagation();
			me._cavTotal();
		})
		
		$("#checkAll").on("click", function(e) {
			if ($(this).hasClass("checked")) {
				$(".myorder .radio").removeClass("checked");
				$(this).removeClass("checked");
			} else {
				$(".myorder .radio").addClass("checked");
				$(this).addClass("checked");
			}
			e.stopPropagation();
			me._cavTotal();
		});
	},
	_initEvent: function() {
		var me = this;
		$(".myorder .mli .btn-plus,.myorder .mli .btn-reduce").on("click", function(e) {
			me._changeNum(e);
		});
		$("#buy-now").click(function() {
			me._submitOrder();
		});
		$(".del_btn").click(function() {
			me._removeOrderDetail(this);
		});
	},
	_submitOrder:function(){
		var me=this;
		var params=window.buyParams;
		var arr = me._getSelected();
		var customerId=$.util.getParam("customerId");
		var dids = arr.dids;
		if(!dids){
			$.util.alert('购物车空空如也');
			return;
		}
		var colors=[];
		var sizes=[];
		$(".pro-list").each(function(){
			var prop=$(this).find(".prop");
			var checkbox = $(this).parent().find(".radio");
			if(prop&&checkbox.hasClass("checked")){
				var color=prop.data("color")||"";
				var size=prop.data("size")||"";
				colors.push(color);
				sizes.push(size);
			}
		});
		colors=colors.join(",");
		sizes=sizes.join(",");
		params.distributionId = window.distributionId;
		if(colors){
			params.colors=colors;
		}
		if(sizes){
			params.sizes=sizes;
		}
		$.req.get("call.json?customerId=" + customerId + "&module=product&method=preSubmitOrder", {
			params: $.extend(params, arr)
		}, function(data) {
			if (data.hasOwnProperty('code')) {
				$("#buy-now").removeClass("btn_disabled");
				$.util.showMsg(data.msg);
			} else {
				var p = {
					"tplid": 1,
					"id": data.id
				}
				location.href = window.hrefPath + '&module=product&force=1&method=getOrderDetail&params=' + encodeURIComponent(JSON.stringify(p));
			}
			$.util.pageLoading.hide();
		});
	},
	_changeNum: function(e) {
		var target = $(e.target),
			did = target.data("did"),
			type = target.data("type"),
			numBox = $("#numBox_" + did),
			num = numBox.val(),
			inventory = numBox.attr("inventory"),
			inventoryCount = numBox.attr("specCount") || numBox.attr("inventoryCount"),
			me = this;
		if (type == "add") {
			if (parseInt(inventory) && inventoryCount) {
				if (parseInt(num,10) < parseInt(inventoryCount,10)) {
					me._updateOrder(did, 1, function() {
						$("#numBox_" + did).val(++num);
					});
				} else {
					$.util.alert('超出库存');
				}
			} else {
				if (parseInt(num,10) < 999) {
					me._updateOrder(did, 1, function() {
						$("#numBox_" + did).val(++num);
					});
				} else {
					$.util.alert('超出最大购买限制');
				}
			}
		} else {
			if (num > 1) {
				me._updateOrder(did, -1, function() {
					$("#numBox_" + did).val(--num);
				});
			}
		}
	},
	_updateOrder: function(did, num, callback) {
		var p = $.util.getParamAsObject();
		var customerId = $.util.getParam("customerId");
		var me=this;
		var pa = {
			"did": did,
			"type": "buy",
			"count": num
		}
		$.req.get("call.json?module=product&customerId=" + customerId + "&method=updateOrderDetail", {
			userName:userName,
			params: pa
		}, function(data) {
			if (!data.code) {
				callback && callback();
				me._cavTotal();
			}
		});
	},
	_getSelected:function(){
		var me = this;
		var dids=[];
		var arr = {};
		var proSource = 0;
		$(".myorder .mli .radio.checked").each(function() {
			dids.push($(this).data("did"));
			if($(this).data("prosource")){
				proSource=$(this).data("prosource");
			}
		});
		arr.dids = dids.join(",");
		arr.proSource = proSource;
		return arr;
	},
	_removeOrderDetail: function(ctl) {
		var me = this;
		var p = $.util.getParamAsObject();
		var customerId = $.util.getParam("customerId");
		var dids =$(ctl).data('id');
		var pa = {
			"dids": dids,
			"type": "buy"
		}
		$.req.get("call.json?module=product&customerId=" + customerId + "&method=removeOrderDetail", {
			userName:userName,
			params: pa
		}, function(data) {
			if (!data.code) {
				var ul = $(ctl).parents('ul');
				$(ctl).parents('li.mli').remove();
				if(ul.find("li").length==0){
					ul.parents('.multi_cart').remove();
				}
				if($(".myorder li.mli").length==0){
					productCart._loadList(productCart.page,function(){
						$.util.pageLoading.hide();
					});
					$("footer").remove();
				}
				me._cavTotal();
			}
		});
	},
	floatMulti: function(arg1, arg2) {
		var m = 0,
			s1 = arg1.toString(),
			s2 = arg2.toString();
		try {
			m += s1.split(".")[1].length
		} catch (e) {}
		try {
			m += s2.split(".")[1].length
		} catch (e) {}
		return (Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m)).toFixed(2);
	},
	floatAdd: function(arg1, arg2) {
		var r1, r2, m;
		try {
			r1 = arg1.toString().split(".")[1].length
		} catch (e) {
			r1 = 0
		}
		try {
			r2 = arg2.toString().split(".")[1].length
		} catch (e) {
			r2 = 0
		}
		m = Math.pow(10, Math.max(r1, r2));
		return ((arg1 * m + arg2 * m) / m).toFixed(2);
	},
	_cavTotal: function() {
		var sum = 0;
		var me = this;
		var nums = 0;
		$(".myorder .mli .radio").each(function() {
			if ($(this).hasClass("checked")) {
				var did = $(this).data("did");
				var numBox = $("#numBox_" + did);
				if(numBox.attr("freeBuy")=='1'){
					var price = numBox.attr("freePrice");
				}else{
					var price = numBox.attr("discountedprice") || numBox.attr("fixedPrice");
				}
				var num = numBox.val();
				if (price && num) {
					var amount=me.floatMulti(price, num);
					sum = me.floatAdd(sum, amount);
					$("#xiaoj_"+did).html("小计：￥"+amount);
					nums += parseInt(num, 10);
				}
			}
		});
		$("#total").text("￥" + sum);
		$("#num").text(nums);
	}
}